<template>
	<view class="contain" :style="{ paddingTop: setmargintop() + 'px' }">
		<view class="new-top-nav">
			<uni-nav-bar title="意见收集" backgroundColor="#fff" color="#000" statusBar="true" @clickLeft="back()"
				left-icon="arrowleft"></uni-nav-bar>
		</view>
		<Province @changeProvince="changeProvince" @changeCity="changeCity" @changeArea="changeArea" @changeAreaLevel="changeAreaLevel" />
		<view class="subsection">
			<scroll-view class="select-top-bar" :scroll-x="true">
				<view v-for="(item, index) in selectViewList" :key="index" @click="selectViewFn(item, index)"
					class="select-top-bar-item" :class="{ 'selected': selected == index }">
					<view class="top-bar-item-title">{{ item.typeName }}</view>
				</view>
			</scroll-view>
		</view>
		<view class="contain-view">
			<!-- search模块 -->
			<!-- <view class="searchs">
				<view class="search">
					<view class="testop">
						<view class="buttime">
							<view class="center">
								<image src="@/static/socialized-service/breed/guanjian.png" mode="aspectFit"></image>
								<uni-easyinput class="uni-mt-5" v-model="searchName" :inputBorder="false"
									placeholder="关键词"></uni-easyinput>
							</view>
						</view>
					</view>
				</view>
				<view class="btn" @click="goSearch">搜索</view>
			</view> -->

			<scroll-view scroll-y="true" :style="{height:setcollapseheight() - 30 + 'px'}" :scroll-top="scrollTop"
				@scroll="onScroll" @scrolltolower="handleScrolltolowerone()">
				<view v-if="dataList.length != 0" class="article-box">
					<view class="article-card" v-for="item,index in dataList" :key="item.policyId"
						@click="loadArticleDetail(item.policyOpinionId)">
						<view class="header">
							<view class="title">
								<!-- {{ index + 1 }}. -->
								<view class="title-name">{{ item.title }}</view>
							</view>
							<!-- <view class="content">{{ item.contentNoImg }}</view> -->
						</view>
						<view class="footer">
							<view class="poster">
								<view class="post">
									<uni-icons type="person" :color="'#ff7c7d'" size="25" class="icon" />
									发布单位：{{ item.department }}
								</view>
							</view>
							<view class="poster">
								<view class="post">
									<uni-icons type="calendar" :color="'#0188ff'" size="25" class="icon" />
									发布日期：{{ item.createTime }}
								</view>
							</view>
						</view>
					</view>
				</view>

				<view v-if="dataList.length == 0" class="facility-view-nodata">
					<image class="facility-view-blankimg" mode="widthFix" src="@/static/blank.png"></image>
					<view class="facility-view-nodata-line">
						暂无数据
					</view>
				</view>
			</scroll-view>
		</view>
		<bottomBar></bottomBar>
	</view>
</template>

<script>
	import Province from '@/subPage1/components/province/index.vue'
	import policiesApi from '@/api/socialized-servce/policies-regulation/index.js'
	import bottomBar from "../components/bottomtabar"

	export default {
		components: {
			bottomBar,
			Province
		},
		data() {
			return {
				selected: 0,
				scrollTop: 0,
				oldScrollTop: 0,
				// 可选择跳转页面数据
				selectViewList: [],
				searchName: '',
				searchDate: [],
				dataList: [],
				techPage: {
					page: 1,
					size: 5,
					total: 0,
				},
				areaLevel: '',
				provinceId: '',
				areaId: '',
				cityId: ''
			}
		},
		onPullDownRefresh() {
			this.searchName = ''
			this.searchDate = []
			this.searchParam = {}
			this.updataContent()
			setTimeout(function() {
				uni.stopPullDownRefresh()
			}, 1000)
		},
		methods: {
			selectViewFn(row, i, flag = false) {
				this.searchName = ''
				this.searchDate = []
				this.searchParam = {}
				this.selected = i
				this.techPage.page = 1
				uni.setStorageSync('policyTypeId', row.policyTypeId)
				this.policyTypeId = row.policyTypeId
				this.policyTypeName = row.typeName
				if(!flag) {
					this.loadDataList()
				}
			},
			onScroll(e) {
				this.oldScrollTop = e.detail.scrollTop
			},
			// 获取知识分类
			loadArticleType() {
				let params = {
					serviceProviderNo: uni.getStorageSync('serviceProviderNo'),
				}
				policiesApi.queryByConditionArticleType(params).then(res => {
					this.selectViewList = res.data
					this.selectViewList.forEach((el,index) =>{
						el.id = index
					})
					if(uni.getStorageSync('policyTypeId')) {
						this.policyTypeId = uni.getStorageSync('policyTypeId')
						let arr = this.selectViewList.filter(el => el.policyTypeId == this.policyTypeId)
						this.selectViewFn(arr[0],arr[0].id,true)
					} else {
						this.policyTypeId = res.data[0].policyTypeId
						this.policyTypeName = res.data[0].typeName
					}
					this.loadDataList()
				})
			},
			// 获取文章列表
			loadDataList() {
				policiesApi.queryPageByConditionOpinion({
					...this.techPage,
					condition: {
						...this.searchParam,
						policyTypeId: this.policyTypeId,
						areaLevel: this.areaLevel,
						provinceId: this.provinceId,
						cityId: this.cityId,
						areaId: this.areaId,
						serviceProviderNo: uni.getStorageSync('serviceProviderNo'),
					}
				}).then(res => {
					let list = res.data.content
					list.map(el => {
						el.contentNoImg = el.content.replace(/<.*?>/ig, "")
					})
					this.techPage.total = res.data.totalElements
					if (this.techPage.page == 1) {
						this.dataList = list
						this.scrollTop = this.oldScrollTop
						this.$nextTick(() => {
							this.scrollTop = 0
						})
					} else {
						this.dataList.push(...list)
					}
				})
			},
			// 搜索
			goSearch() {
				this.searchParam = {
					content: this.searchName,
					// createTime: this.searchDate && this.searchDate+' 00:00:00',
					beginTime: this.searchDate[0] && this.searchDate[0] + ' 00:00:00',
					endTime: this.searchDate[1] && this.searchDate[1] + ' 23:59:59'
				}
				this.updataContent()
			},
			updataContent() {
				this.techPage.page = 1
				this.loadDataList()
			},
			// 上拉加载更多
			handleScrolltolowerone() {
				let len = this.techPage.page * this.techPage.size
				if (len >= this.techPage.total) return
				this.techPage.page += 1
				this.loadDataList()
			},
			// 文章详情
			loadArticleDetail(id) {
				this.$togo('/subPage1/socialized-service/policies-regulation/policy-collect/detail/index', {
					id, policyTypeName: this.policyTypeName
				})
			},
			// 设置顶部高度
			setmargintop() {
				let top = 0
				top = this.customBar + 10
				return top
			},
			//  设置内容高度
			setcollapseheight() {
				let topheightone = uni.upx2px(260)
				let newheight = this.customBar + topheightone
				let winHeight = 0
				uni.getSystemInfo({
					success: function(res) {
						winHeight = res.windowHeight
					}
				});
				winHeight = parseInt(winHeight) - newheight
				// #ifndef H5
				winHeight = winHeight - 10
				// #endif
				return winHeight
			},
			//返回上一步
			back() {
				uni.navigateBack({
					delta: 1
				})
			},
			changeAreaLevel(e) {
				this.areaLevel = e.gradeId
				this.provinceId = null
				this.cityId = null
				this.areaId = null
				if(e.init) {
					this.loadArticleType()
				} else {
					this.loadDataList()
				}
			},
			changeProvince(e) {
				let {provinceId,areaLevel,init} = e
				this.provinceId = provinceId
				this.cityId = null
				this.areaId = null
				this.areaLevel = areaLevel
				if(init) {
					this.loadArticleType()
				} else {
					this.loadDataList()
				}
			},
			changeCity(e) {
				let {provinceId,areaLevel,cityId,init} = e
				this.provinceId = provinceId
				this.cityId = cityId
				this.areaId = null
				this.areaLevel = areaLevel
				if(init) {
					this.loadArticleType()
				} else {
					this.loadDataList()
				}
			},
			changeArea(e) {
				let {provinceId,areaLevel,cityId,areaId,init} = e
				this.provinceId = provinceId
				this.cityId = cityId
				this.areaId = areaId
				this.areaLevel = areaLevel
				if(init) {
					this.loadArticleType()
				} else {
					this.loadDataList()
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.contain {
		width: 100%;
		font-size: 14px;
		line-height: 24px;

		.new-top-nav {
			width: 100%;
			position: fixed;
			top: 0;
			z-index: 99;
		}

		.subsection {
			padding: 0 32rpx;
			margin-bottom: 20rpx;
			border-bottom: 2rpx solid #d3e6f9;

			.select-top-bar {
				width: 100%;
				justify-content: space-around;
				white-space: nowrap;
				display: flex;
				flex-direction: row;
				flex-wrap: nowrap;
				align-items: center;

				.select-top-bar-item {
					display: inline-block;
					margin-right: 20rpx;
					padding-bottom: 12rpx;
					min-width: 162rpx;
					height: 60rpx;
					font-size: 30rpx;

					&.selected {
						border-bottom: 4rpx solid #0e7ae9;
					}

					&:last-child {
						margin-right: 0;
					}

					.top-bar-item-title {
						min-width: 162rpx;
						height: 60rpx;
						line-height: 60rpx;
						text-align: center;
						padding: 0 10px;
					}
				}
			}
		}

		.contain-view {
			margin: 0 30rpx;
			position: relative;

			.searchs {
				position: relative;
				display: flex;
				padding-top: 16rpx;
				align-items: center;
				justify-content: center;
				margin-bottom: 20rpx;

				.search {
					width: 100%;
					height: 80rpx;
					border-radius: 60rpx;
					background-color: rgba(255, 255, 255, 1);
					border: 1px solid rgba(0, 0, 0, 0.05);
					box-shadow: 0px 0px 6px rgba(0, 0, 0, 0.05);
					display: flex;
					justify-content: center;
					align-items: center;
					overflow: hidden;

					.testop {
						// width: 50%;
						width: 100%;
						color: #FFFFFF;
						height: 50rpx;
						border-right: 4rpx #cccbce solid;
						margin-left: 5rpx;
						padding-right: 10rpx;
						font-size: 30rpx;

						&:last-child {
							border-right: none;
						}

						.buttime {
							color: #959497;
							height: 50rpx;
							white-space: nowrap;
							font-weight: 600;
							display: flex;
							justify-content: space-between;
							align-items: center;
							overflow: hidden;
						}

						.center {
							display: flex;
							justify-content: center;
							align-items: center;

							.textt {
								width: 190rpx;
								overflow: hidden;
								text-overflow: ellipsis;
								white-space: nowrap;
							}

							image {
								width: 50rpx;
								height: 50rpx;
								margin-right: 10rpx;
								margin-left: 10rpx;
							}
						}

						::v-deep .uni-date-x {
							padding: 0;
							width: 176rpx;

							.uni-date__x-input {
								padding: 0;
							}

							.icon-calendar {
								display: none;
							}
						}

						::v-deep .uni-date__icon-clear {
							top: 2rpx;
							right: -10rpx;

							.uniui-clear::before {
								font-size: 30rpx;
								color: rgb(192, 196, 204);
							}
						}
					}

					.searchbutton {
						width: 20%;
						background: linear-gradient(18deg, #43A2FF 0%, #62B1FF 100%);
						border-radius: 40rpx;
						box-shadow: none;
						margin-left: 10rpx;
						font-size: 28rpx;
						display: flex;
						justify-content: center;
						align-items: center;
					}
				}

				.btn {
					position: relative;
					z-index: 66;
					width: 130rpx;
					height: 64rpx;
					margin-left: 8rpx;
					font-size: 28rpx;
					line-height: 64rpx;
					text-align: center;
					color: #FFFFFF;
					background: linear-gradient(18deg, #43A2FF 0%, #62B1FF 100%);
					border-radius: 40rpx 40rpx 40rpx 40rpx;
					opacity: 1;
				}
			}

			.article-box {
				overflow: auto;

				.article-card {
					width: 100%;
					background-color: #fff;
					margin-bottom: 20rpx;
					border-radius: 10rpx;
					box-sizing: border-box;

					.header {
						// border-bottom: 1px solid #d3e6f9;
						padding: 20rpx;

						.title {
							font-size: 32rpx;
							font-weight: 500;
							// margin-bottom: 10rpx;
							color: #817539;
							position: relative;
							
							&::before {
								display: inline-block;
								content: '';
								background-color: #817539;
								width: 5px;
								height: 32rpx;
								position: absolute;
								left: -20rpx;
								top: 10rpx;
							}

							.title-name {
								display: inline-block;
								margin-left: 16rpx;
							}
						}

						.content {
							padding-left: 22px;
							max-height: 96px;
							overflow: hidden;
							display: -webkit-box;
							text-overflow: ellipsis;
							-webkit-line-clamp: 4;
							-webkit-box-orient: vertical;
						}
					}

					.footer {
						padding: 0 20rpx 20rpx;
						color: #707273;

						.poster {
							display: flex;

							.post {
								flex: 2;
								width: 40%;
								overflow: hidden;
								text-overflow: ellipsis;
								white-space: nowrap;

								&:last-child {
									flex: 3;
									width: 60%;
								}
								
								.icon {
									vertical-align: middle;
									margin-right: 20rpx;
								}
							}
						}
					}
				}
			}
		}
	}
</style>